<?php
/**
 * login.php
 *
 * UI code for the login popup
 */

add_action( 'login_head', 'myspaceid_wp_login_head');
add_action( 'login_form', 'myspaceid_wp_login_form');

function myspaceid_wp_login_head() {
	$script_path = myspaceid_plugin_url() . '/myspaceid.rev.0.js';
	echo "<script type='text/javascript' src='$script_path'></script>";

	$return_to = openid_service_url('myspaceid', 'consumer', 'login_post');
	$realm = openid_trust_root($return_to);
	$consumer_key = get_option(MYSPACEID_APP_KEY_OPTION);
	/* $redirect_to = get_option('home'); */

	echo <<< LOGIN_SCRIPT_END

		<script type="text/javascript">
	function myspaceid_login() {
		msOptions.realm = '$realm';
		msOptions.returnTo = '$return_to';
		msOptions.consumer = '$consumer_key';
		var ms = new MySpaceID(msOptions);
		ms.logIn();
	}

	function success(redirect_to) {
		//alert(rand);
		window.location.href = redirect_to;
	}

	function failed(rand) {
		/* $('#login div.logout span').addClass("logout_isSignedout"); */
		/* $('#login div.logout span').removeClass("logout_isSignedin"); */
	}
	</script>
LOGIN_SCRIPT_END;
}

function myspaceid_wp_login_form() {
	$button_file = 'myspaceid-php-sdk/source/MySpaceID/images/MySpaceID-loginwith-156x28.png';
	$button_path = myspaceid_plugin_url() . '/' . $button_file;
	
	echo <<< LOGIN_FORM_END

		<a href='#login'
				 onclick='myspaceid_login()' >
		  <img border='0' src='$button_path' alt='Login with MySpaceID' />
		</a>
LOGIN_FORM_END;
}

?>